package cn.christ.com.mobilesafe.view.activity_main;

import android.animation.ObjectAnimator;
import android.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.OnClick;
import cn.christ.com.mobilesafe.R;
import cn.christ.com.mobilesafe.common.BaseActivity;
import cn.christ.com.mobilesafe.common.Global;
import cn.christ.com.mobilesafe.common.utils.SPUtils;
import cn.christ.com.mobilesafe.constant.Constants;
import cn.christ.com.mobilesafe.view.activity_appmanager.AppManagerActivity;
import cn.christ.com.mobilesafe.view.activity_lockscreen.LockScreenActivity;
import cn.christ.com.mobilesafe.view.activity_process.ProcessManagerActivity;
import cn.christ.com.mobilesafe.view.activity_setting.SettingActivity;
import cn.christ.com.mobilesafe.view.activity_sjfd.LostFindActivity;
import cn.christ.com.mobilesafe.view.activity_sjfd.SetingUpActivity1;

public class MainActivity extends BaseActivity {

    @BindView(R.id.iv_main_setting)
    ImageView ivMainSetting;
    @BindView(R.id.lv_main)
    GridView lvMain;
    @BindView(R.id.iv_main_heima)
    ImageView ivMainHeima;
    @BindView(R.id.rl_title)
    RelativeLayout rlTitle;

    private static final int[] IMGRESID = {R.drawable.sjfd, R.drawable.rjgj, R.drawable.jcgl, R.drawable.cxs};
    private static final int[] TITLES = {R.string.sjfd, R.string.rjgj, R.string.jcgl, R.string.cxs};
    private static final int[] DES = {R.string.sjfd, R.string.rjgj, R.string.jcgl, R.string.cxs};
    private static final int DURATION = 1500;

    private EditText etPsw;
    private EditText etRePsw;
    private Button btnOk;
    private Button btnCancel;
    private AlertDialog dialog;

    @Override
    public int getLayoutRes() {
        return R.layout.activity_main;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initListener() {
        lvMain.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position) {
                    case 0: //手机防盗
                        enterSJFD();
                        break;
                    case 1: //软件管家
                        startActivityNotFinish(AppManagerActivity.class);
                        break;
                    case 2: //进程管理
                        startActivityNotFinish(ProcessManagerActivity.class);
                        break;
                    case 3: //程序锁
                        startActivityNotFinish(LockScreenActivity.class);
                        break;
                    default:
                        break;
                }
            }
        });
    }


    @Override
    public void initView() {
        Global.setStatusBarPadding(rlTitle);
        initAnim(); //初始化动画
    }

    @Override
    public void initData() {
        MainAdapter mainAdapter = new MainAdapter(IMGRESID, TITLES, DES);
        lvMain.setAdapter(mainAdapter);
    }

    @OnClick({R.id.iv_main_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_main_setting: //进入设置中心
                startActivityNotFinish(SettingActivity.class);
                break;
        }

    }

    //****************************************************************************************

    /**
     * 初始化动画
     */
    private void initAnim() {
        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(ivMainHeima,
                "rotationY", 0, 90, 270, 360);
        objectAnimator.setDuration(DURATION);
        objectAnimator.setRepeatCount(-1);
        objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
        objectAnimator.start();
    }

    /**
     * 进入手机防盗
     */
    private void enterSJFD() {
        String psw = (String) SPUtils.get(Constants.SJFD_PSW, "-1");
        if (TextUtils.isEmpty(psw) || psw == "-1") {
            //设置密码
            showEnterPswDialog();
        } else {
            //验证密码对话框
            showPswDialog();
        }
    }


    /**
     * 验证密码对话框
     */
    private void showPswDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View inflate = Global.inflate(R.layout.view_set_psw_dialog);

        TextView tv_title = inflate.findViewById(R.id.tv_title);
        etPsw = (EditText) inflate.findViewById(R.id.et_psw);
        etRePsw = (EditText) inflate.findViewById(R.id.et_re_psw);
        btnOk = (Button) inflate.findViewById(R.id.btn_ok);
        btnCancel = (Button) inflate.findViewById(R.id.btn_cancel);

        etRePsw.setVisibility(View.GONE);
        tv_title.setText(Global.getResourcesString(R.string.dialog_title));

        builder.setView(inflate);
        dialog = builder.show();

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String etPsw = MainActivity.this.etPsw.getText().toString().trim();
                if (TextUtils.isEmpty(etPsw)) {
                    Global.showToast(Global.getResourcesString(R.string.et_psw_not_null));
                    return;
                }
                String sp_psw = (String) SPUtils.get(Constants.SJFD_PSW, "-1");
                if (!sp_psw.equals(etPsw)) {
                    Global.showToast(Global.getResourcesString(R.string.et_psw_false));
                    return;
                }
                dialog.dismiss();
                //进入手机防盗的界面
                enterSJFDActivity();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });

    }

    /**
     * 进入手机防盗的界面
     */
    private void enterSJFDActivity() {
        boolean sjfdOpen = (boolean) SPUtils.get(Constants.SJFD_OPEN, false);
        if (sjfdOpen) {
            startActivityNotFinish(LostFindActivity.class);
        } else {
            startActivityNotFinish(SetingUpActivity1.class);
        }

    }


    /**
     * 设置密码
     */
    private void showEnterPswDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        View inflate = Global.inflate(R.layout.view_set_psw_dialog);

        etPsw = (EditText) inflate.findViewById(R.id.et_psw);
        etRePsw = (EditText) inflate.findViewById(R.id.et_re_psw);
        btnOk = (Button) inflate.findViewById(R.id.btn_ok);
        btnCancel = (Button) inflate.findViewById(R.id.btn_cancel);

        builder.setView(inflate);
        dialog = builder.show();

        btnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String etPsw = MainActivity.this.etPsw.getText().toString().trim();
                String etRePsw = MainActivity.this.etRePsw.getText().toString().trim();
                if (TextUtils.isEmpty(etPsw)) {
                    Global.showToast(Global.getResourcesString(R.string.et_psw_not_null));
                    return;
                } else if (TextUtils.isEmpty(etRePsw)) {
                    Global.showToast(Global.getResourcesString(R.string.et_re_psw_not_null));
                    return;
                } else if (!etPsw.equals(etRePsw)) {
                    Global.showToast(Global.getResourcesString(R.string.et_psw_equ_et_re_psw));
                    return;
                }
                //保存密码
                SPUtils.put(Constants.SJFD_PSW, etPsw);
                dialog.dismiss();
            }
        });

        btnCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MainActivity.this.etPsw.setText("");
                MainActivity.this.etRePsw.setText("");
                dialog.dismiss();
            }
        });

    }

}
